window.onload = async function () {
  try {
    const res = await axios({
      url: '/dashboard',
      method: 'get',
    })
    // console.log('res:', res)

    const {
      data: { overview },
    } = res

    // console.log(res)

    // document.querySelector('span[name=salary').innerHTML = overview.salary
    // document.querySelector('span[name=student_count').innerHTML = overview.student_count
    // document.querySelector('span[name=age').innerHTML = overview.age
    // document.querySelector('span[name=group_count').innerHTML = overview.group_count

    for (let key in overview) {
      document.querySelector(`span[name=${key}`).innerHTML = overview[key]
    }

    // 调用折线图函数
    setYearSalary(res.data.year)

    // 调用饼图函数
    setClassSalary(res.data.salaryData)

    // 调用柱状图函数
    setTeamSalary(res.data.groupData)

    // 地图
    initMapChart(res.data.provinceData)

    // 调用双饼图函数
    setGenderSalary(res.data.salaryData)
  } catch (error) {
    console.dir(error)
  }
}

// 创建折线图函数
const setYearSalary = (data) => {
  // 获取元素
  const chartDom = document.querySelector('#line')
  // 初始化图表
  const myChart = echarts.init(chartDom)
  // 设置配置项
  myChart.setOption({
    title: {
      text: '2022全学科薪资走势',
      top: '4%',
      left: '2%',
    },
    xAxis: {
      type: 'category',
      data: data.map((item) => item.month),
      axisLine: {
        lineStyle: {
          type: 'dashed',
          color: '#ccc',
        },
      },
      axisLabel: {
        color: '#999',
      },
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed',
        },
      },
    },
    tooltip: {
      trigger: 'axis',
    },
    grid: {
      top: '20%',
    },
    //设置折线图为渐变颜色
    color: [
      {
        type: 'linear',
        x: 0,
        y: 1,
        x2: 0,
        y2: 0,
        colorStops: [
          {
            offset: 0,
            color: '#499FEE', // 0% 处的颜色
          },
          {
            offset: 1,
            color: '#5D75F0', // 100% 处的颜色
          },
        ],
      },
    ],
    series: [
      {
        data: data.map((item) => item.salary),
        symbolSize: 10,
        lineStyle: {
          width: 6,
        },
        type: 'line',
        smooth: true,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#499FEE',
            },
            {
              offset: 0.8,
              color: 'rgba(255,255,255,0.2)',
            },
            {
              offset: 1,
              color: 'rgba(255,255,255,0)',
            },
          ]),
        },
      },
    ],
  })

  // let option = {
  //   xAxis: {
  //     type: 'category',
  //     data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  //   },
  //   yAxis: {
  //     type: 'value',
  //   },
  //   series: [
  //     {
  //       data: [820, 932, 901, 934, 1290, 1330, 1320],
  //       type: 'line',
  //       smooth: true,
  //     },
  //   ],
  // }

  // // 生成图表
  // option && myChart.setOption(option)
}

// 创建饼图函数
const setClassSalary = (data) => {
  // 获取元素
  const chartDom = document.querySelector('#salary')
  // 初始化图表
  const myChart = echarts.init(chartDom)
  // 设置配置项
  myChart.setOption({
    // 设置图表的标题和字体样式
    title: {
      text: '班级薪资分布',
      top: 15,
      left: 10,
      textStyle: {
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: 'item',
    },
    legend: {
      bottom: '6%',
      left: 'center',
    },
    color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],
    series: [
      {
        name: '班级薪资分布',
        // 设置图标样式
        type: 'pie',
        // 半径:radius
        radius: ['50%', '64%'],
        // 中心点坐标
        center: ['50%', '45%'],
        avoidLabelOverlap: false,
        // 区域样式
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2,
        },
        // 文本
        label: {
          show: false,
          position: 'center',
        },
        emphasis: {
          label: {
            show: false,
            fontSize: '40',
            fontWeight: 'bold',
          },
        },
        // 刻度线(引导线)
        labelLine: {
          show: false,
        },
        data: data.map((item) => {
          return {
            name: item.label,
            value: item.b_count + item.g_count,
          }
        }),
      },
    ],
  })
}

// 创建柱状图函数
const setTeamSalary = (data) => {
  // 获取元素
  const chartDom = document.querySelector('#lines')
  // 初始化图表
  const myChart = echarts.init(chartDom)
  // 设置配置项
  let option = {
    xAxis: {
      type: 'category',
      data: data['1'].map((item) => item.name),
      axisLine: {
        lineStyle: {
          type: 'dashed',
          color: '#ccc',
        },
      },
      axisLabel: {
        color: '#999',
      },
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed',
        },
      },
    },
    tooltip: {
      trigger: 'item',
    },
    color: [
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
            offset: 0,
            color: '#34D39A', // 0% 处的颜色
          },
          {
            offset: 1,
            color: 'rgba(52,211,154,0.2)', // 100% 处的颜色
          },
        ],
      },
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
            offset: 0,
            color: '#499FEE', // 0% 处的颜色
          },
          {
            offset: 1,
            color: 'rgba(73,159,238,0.2)', // 100% 处的颜色
          },
        ],
      },
    ],
    series: [
      {
        data: data[1].map((item) => item.hope_salary),
        type: 'bar',
        name: '期望薪资',
      },
      {
        data: data[1].map((item) => item.salary),
        type: 'bar',
        name: '就业薪资',
      },
    ],
  }
  myChart.setOption(option)

  // 获取元素
  const btns = document.querySelector('#btns')

  // 绑定事件委托
  btns.addEventListener('click', function (e) {
    // 判断是否是通过 button 按钮触发
    if ((e.target.tagName = 'BUTTON')) {
      // console.log(e.target.innerHTML)
      btns.querySelector('.btn-blue')?.classList.remove('btn-blue')
      e.target.classList.add('btn-blue')

      let group = e.target.innerHTML
      // console.log(data[group])
      option.xAxis.data = data[group].map((item) => item.name)
      option.series[0].data = data[group].map((item) => item.hope_salary)
      option.series[1].data = data[group].map((item) => item.salary)

      // 生成图表
      myChart.setOption(option)
    }
  })
}

// 地图
const initMapChart = (provinceData) => {
  const myEchart = echarts.init(document.querySelector('#map'))
  const dataList = [
    { name: '南海诸岛', value: 0 },
    { name: '北京', value: 0 },
    { name: '天津', value: 0 },
    { name: '上海', value: 0 },
    { name: '重庆', value: 0 },
    { name: '河北', value: 0 },
    { name: '河南', value: 0 },
    { name: '云南', value: 0 },
    { name: '辽宁', value: 0 },
    { name: '黑龙江', value: 0 },
    { name: '湖南', value: 0 },
    { name: '安徽', value: 0 },
    { name: '山东', value: 0 },
    { name: '新疆', value: 0 },
    { name: '江苏', value: 0 },
    { name: '浙江', value: 0 },
    { name: '江西', value: 0 },
    { name: '湖北', value: 0 },
    { name: '广西', value: 0 },
    { name: '甘肃', value: 0 },
    { name: '山西', value: 0 },
    { name: '内蒙古', value: 0 },
    { name: '陕西', value: 0 },
    { name: '吉林', value: 0 },
    { name: '福建', value: 0 },
    { name: '贵州', value: 0 },
    { name: '广东', value: 0 },
    { name: '青海', value: 0 },
    { name: '西藏', value: 0 },
    { name: '四川', value: 0 },
    { name: '宁夏', value: 0 },
    { name: '海南', value: 0 },
    { name: '台湾', value: 0 },
    { name: '香港', value: 0 },
    { name: '澳门', value: 0 },
  ]
  const array = JSON.parse(JSON.stringify(provinceData).replace(/省|自治区|维吾尔自治区|壮族自治区|回族自治区/g, ''))
  dataList.forEach(item => {
    array.forEach(i => {
      item.name === i.name ? (item.value = i.value) : item
    })
  })

  // dataList.forEach((item) => {
  //   const data = provinceData.find((i) => {
  //     return (
  //       i.name.replace(/省|自治区|维吾尔自治区|壮族自治区|回族自治区/g, '') ===
  //       item.name
  //     )
  //   })
  //   if (data) item.value = data.value
  // })

  let option = {
    title: {
      text: '籍贯分布',
      top: 10,
      left: 10,
      textStyle: {
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} 位学员',
      borderColor: 'transparent',
      backgroundColor: 'rgba(0,0,0,0.5)',
      textStyle: {
        color: '#fff',
      },
    },
    visualMap: {
      min: 0,
      max: 6,
      left: 'left',
      bottom: '20',
      text: ['6', '0'],
      inRange: {
        color: ['#ffffff', '#0075F0'],
      },
      show: true,
      left: 40,
    },
    geo: {
      map: 'china',
      roam: false,
      zoom: 1.0,
      label: {
        normal: {
          show: true,
          fontSize: '10',
          color: 'rgba(0,0,0,0.7)',
        },
      },
      itemStyle: {
        normal: {
          borderColor: 'rgba(0, 0, 0, 0.2)',
          color: '#e0ffff',
        },
        emphasis: {
          areaColor: '#34D39A',
          shadowOffsetX: 0,
          shadowOffsetY: 0,
          shadowBlur: 20,
          borderWidth: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        },
      },
    },
    series: [
      {
        name: '籍贯分布',
        type: 'map',
        geoIndex: 0,
        data: dataList,
      },
    ],
  }
  myEchart.setOption(option)
}

// 双饼图
const setGenderSalary = (data) => {
  // 获取元素
  const chartDom = document.querySelector('#gender')
  // 初始化图表
  const myChart = echarts.init(chartDom)
  const arrayB = data.map((item) => {
    return { name: item.label, value: item.b_count }
  })
  const arrayG = data.map((item) => {
    return { name: item.label, value: item.g_count }
  })
  let array = [arrayB, arrayG]

  // 设置配置项
  myChart.setOption({
    title: [
      {
        text: '男女薪资分布',
        left: '5%',
        top: '5%',
        textStyle: {
          color: '#000',
          fontWeight: 'normal',
          fontSize: 20,
        },
      },
      {
        text: '男生',
        left: '42%',
        top: '45%',
        textStyle: {
          color: '#000',
          fontWeight: 'normal',
          fontSize: 20,
        },
      },
      {
        text: '女生',
        left: '42%',
        top: '87%',
        textStyle: {
          color: '#000',
          fontWeight: 'normal',
          fontSize: 20,
        },
      },
    ],
    color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],
    series: array.map(function (data, idx) {
      var top = idx * 43
      return {
        type: 'pie',
        radius: [35, 60],
        top: top + '%',
        height: '55%',
        left: 'center',
        width: 300,
        itemStyle: {
          borderColor: '#fff',
          borderWidth: 1,
        },
        label: {
          alignTo: 'edge',
          minMargin: 5,
          edgeDistance: 10,
          lineHeight: 15,
        },
        labelLine: {
          length: 10,
          length2: 0,
          maxSurfaceAngle: 100,
        },
        data: data,
      }
    }),
  })
}
